# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------
# $Id: fleshin_tags.py 34 2010-02-24 03:41:45Z semente $
# ----------------------------------------------------------------------------
#
#  Copyright (c) 2007, 2008 Guilherme Mesquita Gondim
#
#  This file is part of django-fleshin.
#
#  django-fleshin is free software under terms of the GNU General
#  Public License version 3 (GPLv3) as published by the Free Software
#  Foundation. See the file README for copying conditions.
#


"""
The ``fleshin.templatetags.fleshin_tags`` module defines a number of
template tags which may be used to work with photos and your
comments.

To access django-fleshin template tags in a template, use the {% load %}
tag::

    {% load fleshin_tags %}
"""

from django import template
from django.conf import settings
from fleshin.models import Photo


register = template.Library()

class PhotoListNode(template.Node):
    def __init__(self, num, var_name, album=None):
        try:
            if album == None:
                self.album = None
            else:
                self.album = int(album)
        except ValueError:
            self.album =  template.Variable(album)
        self.num = int(num)
        self.var_name = var_name

    def render(self, context):
        if self.album != None and type(self.album) != int:
            try:
                self.album = self.album.resolve(context)
                if self.album == None:
                    return ''
            except template.VariableDoesNotExist:
                return ''
        photo_list = Photo.published_on_site.all()
        if self.album != None:
            photo_list = photo_list.filter(album=self.album)
        context[self.var_name] = list(photo_list[:self.num])
        return ''

def do_get_fleshin_photo_list(parser, token):
    """
    Gets photos list and populates the template context with a variable
    containing that value, whose name is defined by the 'as' clause.

    Syntax::

        {% get_fleshin_photo_list [num] (from [album]) as [var_name] %}

    Example usage to get latest photos::

        {% get_fleshin_photo_list 10 as latest_photos %}

    To get photos from album with id `1`::

        {% get_fleshin_photo_list 10 from 1 as photo_list %}

    To get photos from album with a context variable do::

        {% get_fleshin_photo_list 10 from photo.album as photos_from_even_album %}

    Note: The start point is omitted.
    """
    bits = token.contents.split()
    if len(bits) == 4:
        if bits[2] != 'as':
            raise template.TemplateSyntaxError, "Second argument to '%s' tag must be 'as'" % bits[0]
        return PhotoListNode(num=bits[1], var_name=bits[3])
    if len(bits) == 6:
        if bits[2] != 'from':
            raise template.TemplateSyntaxError, "Second argument to '%s' tag must be 'from'" % bits[0]
        if bits[4] != 'as':
            raise template.TemplateSyntaxError, "Fourth argument to '%s' tag must be 'as'" % bits[0]
        return PhotoListNode(num=bits[1], var_name=bits[5], album=bits[3])
    else:
        raise template.TemplateSyntaxError, "'%s' tag takes three or five arguments" % bits[0]


register.tag('get_fleshin_photo_list', do_get_fleshin_photo_list)
